package com.asha.md360player4android.vr.plugins;

import android.content.Context;
import android.opengl.GLES20;

import com.asha.vrlib.MD360Director;
import com.asha.vrlib.MD360Program;
import com.asha.vrlib.model.MDMainPluginBuilder;
import com.asha.vrlib.model.MDPosition;
import com.asha.vrlib.objects.MDAbsObject3D;
import com.asha.vrlib.plugins.MDAbsPlugin;
import com.asha.vrlib.strategy.projection.ProjectionModeManager;
import com.asha.vrlib.texture.MD360Texture;

import static com.asha.vrlib.common.GLUtil.glCheck;

/**
 * Created by hzqiujiadi on 16/7/22.
 * hzqiujiadi ashqalcn@gmail.com
 */
public class FYCalibratePluginEx2 extends MDAbsPlugin {

    private MD360Program mProgram;
    private MD360Texture mTexture;

    private ProjectionModeManager mProjectionModeManager;

    public FYCalibratePluginEx2(MDMainPluginBuilder builder) {
        mTexture = builder.getTexture();
        mProgram = new MD360Program(builder.getContentType());
        mProjectionModeManager = builder.getProjectionModeManager();
    }

    @Override
    public void initInGL(Context context) {
        mProgram.build(context);
        mTexture.create();
    }

    @Override
    public void beforeRenderer(int totalWidth, int totalHeight) {
        drawConverter(totalWidth, totalHeight);
    }

    @Override
    public void renderer(int index, int width, int height, MD360Director director) {

        MDAbsObject3D object3D = mProjectionModeManager.getObject3D();
        // check obj3d
        if (object3D == null) return;

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        glCheck("FYMDPanoramaPlugin2 renderer glClear");

        // Update Projection
        director.updateViewport(width, height);

        // Set our per-vertex lighting program.
        glCheck("FYMDPanoramaPlugin2 mBitmapProgram use");

        object3D.uploadVerticesBufferIfNeed(mProgram, index);
        object3D.uploadTexCoordinateBufferIfNeed(mProgram, index);

        // Pass in the combined matrix.
        // model view projection matrix.
        director.beforeShot();
        director.shot(mProgram, getModelPosition());
        object3D.draw();

    }

    @Override
    public void destroyInGL() {
        mTexture = null;
    }

    @Override
    protected MDPosition getModelPosition() {
        return mProjectionModeManager.getModelPosition();
    }

    @Override
    protected boolean removable() {
        return false;
    }


    private void drawConverter(int width, int height) {

        MD360Director director = mProjectionModeManager.getMD360DirectorPlus();
        if (null == director) {
            return;
        }

        MDAbsObject3D object3D = mProjectionModeManager.getObject3DPlus();
        // check obj3d
        if (object3D == null) return;

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        glCheck("FYMDPanoramaPlugin2 drawConverter glClear");

        // Update Projection
        director.updateViewport(width, height);

        GLES20.glViewport(0, 0, width, height);
        GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
        GLES20.glScissor(0, 0, width, height);

        mProgram.use();
        mTexture.texture(mProgram);

        object3D.uploadVerticesBufferIfNeed(mProgram, 0);
        object3D.uploadTexCoordinateBufferIfNeed(mProgram, 0);

        // Pass in the combined matrix.
        director.beforeShot();
        director.shot(mProgram, mProjectionModeManager.getModelPositionPlus());
        object3D.draw();

        GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
    }
}
